/**数据验证*/
var UserValidator = function() {  
    var handleSubmit = function() { 
        $('.form-horizontal').validate({  
            errorElement : 'span',  
            errorClass : 'help-block',  
            focusInvalid : false,  
            rules : {  
            	"user.loginid" : {  
                    required : true  
                },  
                "user.name" : {  
                    required : true  
                },
                "user.tel"  :{
                	required : true,
                	digits:true
                }
            },  
            messages : {  
            	"user.loginid" : {  
                    required : "请输入登录名."  
                },  
                "user.name" : {  
                    required : "请输入姓名."  
                },
                "user.tel"  :{
                	required : "请输入电话"
                }
            },  
  
            highlight : function(element) { 
                $(element).closest('.control-group').addClass('error');  
            },  
  
            success : function(label) { 
                label.closest('.control-group').removeClass('error');  
                label.remove();
            },  
  
            errorPlacement : function(error, element) {  
            	element.parent('div').append(error);  
            },  
  
            submitHandler : function(form) {
                form.submit();
            }  
        });  
  
        $('.form-horizontal input').keypress(function(e) {  
            if (e.which == 13) {  
                if ($('.form-horizontal').validate().form()) {  
                    $('.form-horizontal').submit();  
                }  
                return false;  
            }  
        });  
    }  
    return {  
        init : function() { 
            handleSubmit(); 
        }  
    };  
  
}(); 


/**保存数据*/
function saveUser(){
	$('#myModal').modal('show');
	var loginid=$("#loginid").val();
	var name=$("#name").val();
	var tel=$("#tel").val();
	$.post("../system/save_User.action",{"user.loginid":loginid,"user.name":name,
		"user.tel":tel}, function(result) {
		if(result.msg=="succ"){
			$('#myModal').modal('hide');
			$("#alertinfo").html("用户维护成功，现在可对该用户设置密码和设置权限");
			$('#myinfo').modal('show');
			$("#loginid").attr("disabled",true);
		}else{
			$('#myModal').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
};
/**数据验证*/
var PwdValidator = function() {  
    var handlePwdSubmit = function() { 
        $('#pwdform').validate({  
            errorElement : 'span',  
            errorClass : 'help-block',  
            focusInvalid : false,  
            rules : {  
            	"pwd" : {  
                    required : true  
                },  
                "confirm_pwd" : {  
                    required : true  
                }
            },  
            messages : {  
            	"pwd" : {  
                    required : "请输入密码."  
                },  
                "confirm_pwd" : {  
                    required : "请输入确认密码."  
                }
            },  
  
            highlight : function(element) { 
                $(element).closest('.control-group').addClass('error');  
            },  
  
            success : function(label) { 
                label.closest('.control-group').removeClass('error');  
                label.remove();
            },  
  
            errorPlacement : function(error, element) {  
            	element.parent('div').append(error);  
            },  
  
            submitHandler : function(form) {
                form.submit();
            }  
        });  
  
        $('#pwdform input').keypress(function(e) {  
            if (e.which == 13) {  
                if ($('#pwdform').validate().form()) {  
                	$('#pwdform').submit();  
                }  
                return false;  
            }  
        });  
    }  
    return {  
        init : function() { 
        	handlePwdSubmit(); 
        }  
    };  
  
}(); 
function openedit(){
	if($("#loginid").attr("disabled")){
		$('#pwd_loginid').val($('#loginid').val());
		$('#editpwd').modal('show');
	}else{
		alert("请先保存用户信息，再进行密码设置.");
	}
}
function editpermission(){
	if($("#loginid").attr("disabled")){
		var loginid=$('#loginid').val();
		window.location.assign('../system/editPermission.action?loginid='+loginid);
	}else{
		alert("请先保存用户信息，再进行权限设置.");
	}
}
function savePwd(){
	var loginid=$("#pwd_loginid").val();
	var pwd=$("#pwd").val();
	$.post("../system/savePwd_User.action",{"user.loginid":loginid,"user.pwd":pwd}, function(result) {
		if(result.msg=="succ"){
			$('#editpwd').modal('hide');
			$("#alertinfo").html("用户密码设置成功。");
			$('#myinfo').modal('show');
		}else{
			$('#editpwd').modal('hide');
			$("#alertinfo").html(result.msg);
			$('#myinfo').modal('show');
		}
	});
}
$(document).ready(function(){
	UserValidator.init();
	PwdValidator.init();
});
